package com.itbaizhan.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itbaizhan.common.BaseResponse;
import com.itbaizhan.entity.TStorkage;
import com.itbaizhan.mapper.TStorkageMapper;
import com.itbaizhan.service.ITStockService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 库存业务层
 */
@DubboService
public class ITStockServiceImpl implements ITStockService {


    @Autowired
    private TStorkageMapper tStorkageMapper;


    @Override
    public BaseResponse deduct(String commodityCode, Integer count) {
        //查询库存
        LambdaQueryWrapper<TStorkage> tStorkageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tStorkageLambdaQueryWrapper.eq(TStorkage::getCommodityCode,commodityCode);
        TStorkage tStorkage = tStorkageMapper.selectOne(tStorkageLambdaQueryWrapper);
        if (tStorkage == null ){
            return BaseResponse.error("商品不存在");
        }
        if (count >  tStorkage.getCount()){
            return BaseResponse.error("库存不足");
        }
        tStorkage.setCount(tStorkage.getCount() - count);
        int update = tStorkageMapper.updateById(tStorkage);
        if (update > 0){
            return BaseResponse.success();
        }

        return BaseResponse.error("服务异常");
    }




}
